<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <el-form :model="objForm" :rules="rules" ref="objForm" label-width="100px" class="u-form">
        <el-input v-model="objForm.id" type="hidden" style="display: none;"></el-input>
        <el-form-item label="选择商品">
          <el-input
            v-model="objForm.goods_id"
            placeholder="商品id"
            type="hidden"
            style="display: none;"
          ></el-input>
          <div v-if="objForm.goods_id>0">
            <el-input v-model="objForm.goods_title" placeholder="商品标题" disabled></el-input>
            <el-image
              style="max-width: 150px;"
              :src="objForm.goods_img"
              :data-src="objForm.goods_img"
              fit="cover"
              lazy
            ></el-image>
          </div>
          <el-button type="text" @click="clickGoodsSelect()">选择商品</el-button>
        </el-form-item>
        <el-form-item label="团购价">
          <el-input v-model="objForm.sell_price" placeholder="团购价"></el-input>
        </el-form-item>
        <el-form-item label="团购人数">
          <el-input v-model="objForm.condition" placeholder="团购人数"></el-input>
        </el-form-item>
        <el-form-item label="团购有效时间">
          <el-col :span="18">
            <el-input type="text" v-model="objForm.condition_time" placeholder="单位（小时）"></el-input>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="4">单位（小时）</el-col>
        </el-form-item>
        <el-form-item label="商品数量" v-if="objForm.id==''">
          <el-input v-model="objForm.goods_storage" placeholder="商品数量"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="objForm.sort" :min="0" label="数值越大权重越高"></el-input-number>
        </el-form-item>
        <el-form-item label="是否上线" prop="status">
          <el-switch
            v-model="objForm.status"
            active-color="#13ce66"
            :active-value="1"
            inactive-color="#666"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 商品弹出框 -->
    <el-dialog title="商品列表" :visible.sync="is_goods_show" width="55%" append-to-body>
      <Goods :isprops="true" @goods_select="goods_select" :activity="aid" ref="goodslist"></Goods>

      <span slot="footer" class="dialog-footer">
        <el-button @click="is_goods_show = false">取 消</el-button>
        <el-button type="primary" @click="is_goods_show = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import commonEdit from "@/mixins/common-edit";
import Goods from "@/views/goods/list.vue";
export default {
  components: {
    Goods
  },
  props: {
    aid: {
      type: String
    },
    time: {
      type: Number,
      value: 24
    },
    id: {
      type: Number,
      value: 0
    }
  },
  mixins: [commonEdit],
  data() {
    return {
      apiName: "activityGoods",
      objForm: {
        id: "",
        activity_id: "", //活动id
        goods_id: "", //商品id
        sell_price: 0,
        condition: 2,
        condition_time: 1, //团购有效时间
        goods_storage: 0,
        sort: 0, //排序
        status: 1
      },
      is_goods_show: false,
      resetInit: true
    };
  },
  methods: {
    beforeCreated() {
      if (this.id) {
        this.objForm.id = this.id;
        this.getInfo();
      }
      this.objForm.condition_time = this.time;
    },
    goods_select(row) {
      this.objForm.id = "";
      this.objForm.activity_id = this.aid;
      // console.log(this.aid);
      //商品选择
      // window.console.log(row);
      this.objForm.goods_id = row.id;
      this.objForm.goods_title = row.goods_title;
      this.objForm.goods_img = row.goods_img;
      this.is_goods_show = false; //关闭弹框
    },
    submitEnd(code) {
      if (code == 1) {
        this.$emit("submit", {});
        this.objForm.goods_id = "";
        this.objForm.goods_title = "";
        this.objForm.goods_img = "";
        this.objForm.sell_price = 0;
        this.objForm.condition = 2;
        this.objForm.condition_time = 1;
        this.objForm.goods_storage = 0;
        this.objForm.sort = 0; //排序
        this.objForm.status = 1;
      }
    },
    clickSub() {
      let self = this;
      self.submitForm("objForm");
    },
    clickGoodsSelect() {
      this.is_goods_show = true;
      let that = this;
      setTimeout(function() {
        that.$refs["goodslist"].upDateList();
      }, 500);
    }
  }
};
</script>
